/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sun.courier.view.support;

import com.sun.courier.model.*;
import com.sun.courier.dal.CourierDAL;
import com.sun.courier.dto.Courier_CourierManageDTO;
import com.sun.courier.entity.Courier;
import com.sun.courier.entity.CourierManage;
import com.sun.courier.view.CourierActions;
import com.sun.courier.view.DestinationStoreActions;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.EventObject;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ButtonModel;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.event.CellEditorListener;
import javax.swing.event.ChangeEvent;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;

/**
 *
 * @author HONG QUAN
 */
public class DestinationStore_ButtonsEditor extends DestinationStore_ButtonsPanel implements TableCellEditor {

    transient protected ChangeEvent changeEvent = null;
    DestinationStoreActions dsa=null;
    CourierDAL courierDal = new CourierDAL();
    
    public DestinationStore_ButtonsEditor(final JTable table) {
        super();

        //---->
        //DEBUG: view button click -> control key down + edit button(same cell) press -> remain selection color
        MouseListener ml = new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                ButtonModel m = ((JButton) e.getSource()).getModel();
                if (m.isPressed() && table.isRowSelected(table.getEditingRow()) && e.isControlDown()) {
                    setBackground(table.getBackground());
                }
            }
        };
        buttons.get(0).addMouseListener(ml);
        //<----

        buttons.get(0).addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //Object o = table.getModel().getValueAt(table.getSelectedRow(), 0);
                int row = table.convertRowIndexToModel(table.getEditingRow());
                ArrayList<Courier_CourierManageDTO> lst = new ArrayList<>();
                Courier c = new Courier();
                c.setCourierId(Integer.parseInt(table.getModel().getValueAt(row, 0).toString()));
                c.setCourierName(table.getModel().getValueAt(row, 1).toString());
                c.setCustomerName(table.getModel().getValueAt(row, 2).toString());
                c.setPhone(table.getModel().getValueAt(row, 3).toString());

                CourierManage cm = new CourierManage();
                cm.setSender(3);// Nguoi gui luc nay la nguoi login // Nguoi nhan phai nhap username vao.
                Courier_CourierManageDTO cmDto = new Courier_CourierManageDTO();
                cmDto.setC(c);
                cmDto.setCm(cm);
                lst.add(cmDto);
                fireEditingStopped();
                if (dsa == null) {
                    try {
//                        dsa = new DestinationStoreActions((DefaultTableModel)table.getModel(),row);
                    } catch (Exception ex) {
                        Logger.getLogger(DestinationStore_ButtonsEditor.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    dsa.setValues(lst);
                    dsa.setAlwaysOnTop(true);
                    dsa.setSize(new Dimension(600, 500));
                    dsa.setVisible(true);
                } else {
                    dsa.setValues(lst);
                    dsa.invalidate();
                    dsa.validate();
                    dsa.repaint();
                    dsa.setAlwaysOnTop(true);
                    dsa.setVisible(true);
                }
            }
        });

        addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                fireEditingStopped();
            }
        });
    }

    @Override
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
        this.setBackground(table.getSelectionBackground());
        return this;
    }

    @Override
    public Object getCellEditorValue() {
        return "";
    }

    //Copid from AbstractCellEditor
    //protected EventListenerList listenerList = new EventListenerList();
    //transient protected ChangeEvent changeEvent = null;
    @Override
    public boolean isCellEditable(EventObject e) {
        return true;
    }

    @Override
    public boolean shouldSelectCell(EventObject anEvent) {
        return true;
    }

    @Override
    public boolean stopCellEditing() {
        fireEditingStopped();
        return true;
    }

    @Override
    public void cancelCellEditing() {
        fireEditingCanceled();
    }

    @Override
    public void addCellEditorListener(CellEditorListener l) {
        listenerList.add(CellEditorListener.class, l);
    }

    @Override
    public void removeCellEditorListener(CellEditorListener l) {
        listenerList.remove(CellEditorListener.class, l);
    }

    public CellEditorListener[] getCellEditorListeners() {
        return listenerList.getListeners(CellEditorListener.class);
    }

    protected void fireEditingStopped() {
        // Guaranteed to return a non-null array
        Object[] listeners = listenerList.getListenerList();
        // Process the listeners last to first, notifying
        // those that are interested in this event
        for (int i = listeners.length - 2; i >= 0; i -= 2) {
            if (listeners[i] == CellEditorListener.class) {
                // Lazily create the event:
                if (changeEvent == null) {
                    changeEvent = new ChangeEvent(this);
                }
                ((CellEditorListener) listeners[i + 1]).editingStopped(changeEvent);
            }
        }
    }

    protected void fireEditingCanceled() {
        // Guaranteed to return a non-null array
        Object[] listeners = listenerList.getListenerList();
        // Process the listeners last to first, notifying
        // those that are interested in this event
        for (int i = listeners.length - 2; i >= 0; i -= 2) {
            if (listeners[i] == CellEditorListener.class) {
                // Lazily create the event:
                if (changeEvent == null) {
                    changeEvent = new ChangeEvent(this);
                }
                ((CellEditorListener) listeners[i + 1]).editingCanceled(changeEvent);
            }
        }
    }
}
